package com.kumi.crowd.controller;

import com.github.pagehelper.PageInfo;
import com.kumi.crowd.bean.Role;
import com.kumi.crowd.service.RoleService;
import com.kumi.crowd.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping("info")
    @ResponseBody
    public Result<PageInfo<Role>> getInfo(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
            @RequestParam(value = "keyword",defaultValue = "") String keyword){

        PageInfo<Role> info = null;
        try {
            info = roleService.getInfo(pageNum, pageSize, keyword);
            return Result.successData(info);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(e.getMessage());
        }
    }

    @RequestMapping("/add")
    @ResponseBody
    public Result<String> add(Role role){
        roleService.add(role);
        return Result.success();
    }

    @RequestMapping("/update")
    @ResponseBody
    public Result<String> update(Role role){
        roleService.update(role);
        return Result.success();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Result<String> remove(@RequestBody List<Integer> roleList) {
        System.out.println(roleList);
        roleService.removeRole(roleList);
        return Result.success();
    }

}
